package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @Author: WangHongKun
 * @Date: 2022/9/22 16:58
 * @Email: 2028911483@qq.com
 * @Phone: 18683977706
 */

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {

    @Autowired
    private  DishService  dishService;
    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 清除 Redis缓存
     * @param pattern
     */
    public void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }



    /**
     * 增加菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("增加菜品接口")
    public Result addDish(@RequestBody DishDTO dishDTO){
        log.info("增加菜品：{}",dishDTO);
        dishService.addDish(dishDTO);
        String key = "dish_" +dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询接口")
    public  Result<PageResult<DishVO>>  PageQuery(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询开始：{}",dishPageQueryDTO);
        PageResult<DishVO> pageResult = dishService.pageQuery(dishPageQueryDTO);
        return  Result.success(pageResult);
    }


    @DeleteMapping
    @ApiOperation("删除菜品接口")
    public  Result  Delete(String ids){
        log.info("删除的菜品ids:{}",ids);
        dishService.DeleteAll(ids);
        cleanCache("dish_*");
        return Result.success();
    }



    @PostMapping("/status/{status}")
    @ApiOperation("菜品停售起售接口")
    public Result startOrStop(Long id , @PathVariable(name = "status") Integer status){
        log.info("菜品停售起售");
        dishService.startOrStop(id,status);
        DishVO dishVO = dishService.getById(id);
        String key = "dish_"+ dishVO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }


    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品接口")
    public Result Update(@RequestBody DishDTO dishDTO){
        log.info("传递的修改后菜品信息：{}",dishDTO);
        dishService.Update(dishDTO);
        String key = "dish_"+ dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * @return
     */
    @ApiOperation("根据id查询菜品接口")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("查询的菜品id：{}",id);
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }


    /**
     * 根据分类id查询菜品
     * @return
     */
    @ApiOperation("根据分类id查询菜品接口")
    @GetMapping("/list")
    public Result<List<Dish>> getByCategoryId(Long categoryId) {
        log.info("查询的分类id：{}",categoryId);
        List<Dish> dishList = dishService.getByCategoryId(categoryId);
        return Result.success(dishList);
    }
}
